$(function(){
    obj.init();
});

var obj = new JsFun();
function JsFun(){
    'use strict';
    return {
        dom: {
            btnView: null,
            frame: null,
            school: null,
            area: null,
            start: null,
            end: null,
            slClass: null,
            subject: null
        },
        init: function () {
            this.dom.btnView = $('#btn_view');
            this.dom.frame   = $('iframe');
            this.dom.school  = $('#school_id');
            this.dom.area    = $('#school_area');
            this.dom.start   = $('#startTime');
            this.dom.end     = $('#endTime');
            this.dom.slClass = $('#class_id');
            this.dom.subject = $('#subject');

            this.dom.school.select2();

            this.listener();
        },
        listener: function () {
            var $this = this;
            //点击查询
            $this.dom.btnView.click(function () {
                $this.funObj().viewUsage();
            });
            //加载完毕后关闭提示
            $this.dom.frame.load(function(){
                window.top.swal.close();
            });
            //绑定学校行政班
            $this.dom.school.change(function () {
                $this.funObj().dealSchoolClass($(this).val());
            });
            //绑定区域
            $this.funObj().bindAreaSchool();
        },
        funObj: function(){
            var $this = this;
            return {
                viewUsage: function () {
                    $this.dom.frame.attr('src', '');
                    setTimeout(function () {
                        var schoolId    = $this.dom.school.val(),
                            classId     = $this.dom.slClass.val(),
                            subject     = $this.dom.subject.val(),
                            startTime   = $this.dom.start.val(),
                            endTime     = $this.dom.end.val();
                        if (!schoolId) {
                            window.top.xhTop.showError('学校为必选项！');
                            xhExtra.setBtnNormal($this.dom.btnView);
                            return false;
                        }
                        if (!classId) {
                            window.top.xhTop.showError('班级为必选项！');
                            xhExtra.setBtnNormal($this.dom.btnView);
                            return false;
                        }
                        if (!startTime || !endTime) {
                            window.top.xhTop.showError('时间不能为空！');
                            xhExtra.setBtnNormal($this.dom.btnView);
                            return false;
                        }
                        $this.dom.frame.attr('src',
                            baseUrl + 'Statsschool/brushGroupDetail?'
                            + xhTool.parseParam({schoolId: schoolId, classId: classId, subject: subject, startTime: startTime, endTime: endTime})
                        );
                        xhExtra.setBtnNormal($this.dom.btnView);
                        window.top.swal({
                            title: '统计执行中！请耐心等待...',
                            showConfirmButton: false,
                            type: 'success',
                            allowOutsideClick: false
                        });
                        setTimeout(function () {
                            window.top.swal.close();
                        }, 30 * 1000);
                    }, 0);
                },
                bindAreaSchool: function () {
                    if ($this.dom.area.length >= 1) {
                        xhExtra.ajaxRequest('Other/getDep', {tag: 'XHYYB', depth: 2}, {check: true}, function (data) {
                            data.unshift({id: 0, tag: '全部', text: '-全部-'});
                            $this.dom.school.trigger('change');
                            $this.dom.area.select2({data: data}).change(function () {
                                xhExtra.ajaxRequest('Other/getSchoolByDep', {depId: $(this).val()}, {check: false}, function (schools) {
                                    if (xhExtra.checkError(schools)) {
                                        $this.dom.school.empty();
                                        $this.dom.slClass.empty().select2({placeholder: '班级'});
                                        return false;
                                    }
                                    $this.dom.school.empty().select2({data: schools});
                                    $this.dom.school.trigger('change');
                                });
                            });//.trigger('change');
                        });
                    }
                    else {
                        $this.dom.school.select2();
                        $this.dom.school.change();
                    }
                },
                dealSchoolClass: function (schoolId) {
                    xhExtra.ajaxRequest('Other/getSchoolClass', {schoolId: schoolId}, {check: false}, function (data) {
                        if (xhExtra.checkError(data)) {
                            $this.dom.slClass.empty().select2({placeholder: '班级'});
                            return false;
                        }
                        $this.dom.slClass.empty().select2(data.length ? {data: data} : {placeholder: '班级'});
                    });
                }
            }
        }
    }
}